<?php

/**
 * ECSHOP 首页文件
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: index.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}
$ua = strtolower($_SERVER['HTTP_USER_AGENT']);

$uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i";

if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap'))
{
    $Loaction = 'mobile/';

    if (!empty($Loaction))
    {
        ecs_header("Location: $Loaction\n");

        exit;
    }
}

/*------------------------------------------------------ */
//-- 判断是否存在缓存，如果存在则调用缓存，反之读取相应内容
/*------------------------------------------------------ */
/* 缓存编号 */
$cache_id = sprintf('%X', crc32($_SESSION['user_rank'] . '-' . $_CFG['lang']));

if (!$smarty->is_cached('index.dwt', $cache_id))
{
    assign_template();

    $position = assign_ur_here();
    $smarty->assign('page_title',      $position['title']);    // 页面标题
    $smarty->assign('ur_here',         $position['ur_here']);  // 当前位置

    /* meta information */
    $smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords']));
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc']));
    $smarty->assign('flash_theme',     $_CFG['flash_theme']);  // Flash轮播图片模板
	$smarty->assign('new_goods',       get_recommend_goods('new'));     // 最新商品
	$smarty->assign('hot_goods',       get_recommend_goods('hot'));     // 热卖商品

    /* 首页主广告设置 */
    $smarty->assign('index_ad',     $_CFG['index_ad']);
    if ($_CFG['index_ad'] == 'cus')
    {
        $sql = 'SELECT ad_type, content, url FROM ' . $ecs->table("ad_custom") . ' WHERE ad_status = 1';
        $ad = $db->getRow($sql, true);
        $smarty->assign('ad', $ad);
    }
	
	$arr_cate = '1,2,3,4,5,6,7,8';
	$smarty->assign('category_goods',		   get_category($arr_cate));
    /*echo '<pre>';
    print_r(get_category($arr_cate));*/
	
    /* 页面中的动态内容 */
    assign_dynamic('index');
}

$smarty->display('index.dwt', $cache_id);

function get_category($arr_cate)
{
	$sql = 'SELECT cat_id,cat_name FROM '. $GLOBALS['ecs']->table("category").' WHERE cat_id IN ('.$arr_cate.')';
	$res = $GLOBALS['db']->getAll($sql);
	$cat_arr = array();
	$cat_index = 1;
	foreach($res as $row)
	{
        $cat_arr[$row['cat_id']]['cat_id'] = $row['cat_id'];
		$cat_arr[$row['cat_id']]['cat_name'] = $row['cat_name'];
		$cat_arr[$row['cat_id']]['cat_index'] = $cat_index;
		$cat_arr[$row['cat_id']]['cat_url'] = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
		$cat_arr[$row['cat_id']]['cat_goods'] = get_category_goods($row['cat_id'],6);
        /*是否有子分类*/
        $sql = 'SELECT cat_id,cat_name FROM '.$GLOBALS['ecs']->table("category").' WHERE parent_id ='.$row['cat_id'];
        $child_cat = $GLOBALS['db']->getAll($sql);
        if($child_cat)
        {
            foreach ($child_cat as $k => $v) {
                $cat_arr[$row['cat_id']]['cat_childcat'][$k]['cat_name'] = $v['cat_name'];
                $cat_arr[$row['cat_id']]['cat_childcat'][$k]['cat_url'] = build_uri('category', array('cid' => $v['cat_id']), $v['cat_name']);
            }
        }
		$cat_index++;
	}
	return $cat_arr;
}


//获取分类下的商品
function get_category_goods($cat_id,$limit)
{
	$sql = 'SELECT * FROM '. $GLOBALS['ecs']->table("goods").' WHERE cat_id = '.$cat_id.' LIMIT 0,'.$limit;
	$res = $GLOBALS['db']->getAll($sql);
	$goods = array();
	$goods_index = 1;
	foreach($res as $row)
	{
		$goods[$row['goods_id']]['goods_id'] = $row['goods_id'];
		$goods[$row['goods_id']]['goods_index'] = $goods_index;
		$goods[$row['goods_id']]['goods_url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
		$goods[$row['goods_id']]['goods_name'] = $row['goods_name'];
		$goods[$row['goods_id']]['goods_thumb'] = $row['goods_thumb'];
		$goods_index++;
	}
	return $goods;
}
?>
